<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/11
 * Time: 11:42
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\User;
use common\models\DAO\UserInvest;

class RankService {
    public static function init(){
        $result['brokerage'] = self::brokerage();
        $result['money'] = self::money();
        return $result;
    }

    public static function money($page = 1, $limit = 10){
        $page <= 0 && $page = 1;
        $start = (intval($page) - 1) * $limit;
        $money = UserInvest::find()->innerJoin('p2p_user','p2p_user.id=p2p_user_invest.userID')
            ->groupBy('p2p_user_invest.userID')->asArray()
            ->select('p2p_user_invest.id,sum(p2p_user_invest.money) as amount, p2p_user.phone')->limit($limit)->offset($start)
            ->orderBy('amount desc')
            ->all();
        foreach($money as $k=>$v){
            $money[$k]['phone'] = CommonFunction::dealTel(CommonFunction::decrypt($v['phone']));
            $money[$k]['amount'] = CommonFunction::formatMoney($v['amount']);
        }
        return $money;
    }

    public static function brokerage($page = 1, $limit = 10){
        $page <= 0 && $page = 1;
        $start = (intval($page) - 1) * $limit;
        $brokerage = User::find()->innerJoin('p2p_user u', 'u.recID=p2p_user.id')->groupBy('p2p_user.id')->asArray()->
        select('COUNT(u.id) AS c ,p2p_user.phone, p2p_user.id')->limit($limit)->offset($start)->orderBy('c desc')
            ->all();
        foreach($brokerage as $k=>$v){
            $brokerage[$k]['phone'] = CommonFunction::dealTel(CommonFunction::decrypt($v['phone']));
        }
        return $brokerage;
    }
}